package com.wdz.main_category.mapper;

import com.wdz.article.entity.Article;
import com.wdz.cache.RedisCache;
import com.wdz.main_category.entity.MainCategory;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 主分类表 Mapper 接口
 * </p>
 *
 * @author 吴传逞
 * @since 2020-12-19
 */
@Mapper
@CacheNamespace(implementation= RedisCache.class,eviction=RedisCache.class)
public interface MainCategoryMapper extends BaseMapper<MainCategory> {



    @Select("SELECT DISTINCT d.* FROM  category a LEFT JOIN category b ON b.parent_category_id = a.id LEFT JOIN category c ON c.parent_category_id = b.id RIGHT JOIN article d ON d.parent_category_id = c.id OR d.relation_category_ids like CONCAT('%',c.id,'%') WHERE a.`level`=0 and a.main_category_id=#{id} and d.type = 0 ORDER BY d.is_topping desc,d.create_date DESC LIMIT ${pageNo},${pageSize}")
    List<Article> articles(@Param("id") String id,@Param("pageNo") Integer pageNo,@Param("pageSize") Integer pageSize);
    @Select("SELECT count(DISTINCT d.title,d.parent_category_id) FROM  category a LEFT JOIN category b ON b.parent_category_id = a.id\n" +
            "                                   LEFT JOIN category c ON c.parent_category_id = b.id RIGHT JOIN article d ON d.parent_category_id = c.id\n" +
            "            OR d.relation_category_ids like CONCAT('%',c.id,'%')\n" +
            "        WHERE a.`level`=0 and a.main_category_id=#{id} and d.type = 0")
    Integer articlesCount(@Param("id") String id);
    @Select("SELECT DISTINCT d.* FROM  category a LEFT JOIN category b ON b.parent_category_id = a.id LEFT JOIN category c ON c.parent_category_id = b.id RIGHT JOIN article d ON d.parent_category_id = c.id OR d.relation_category_ids like CONCAT('%',c.id,'%') WHERE a.`level`=0 and a.main_category_id=#{id} and d.type = 0 and d.is_topping = 1 ORDER BY count DESC  LIMIT 0,10")
    List<Article> articlesTopping(@Param("id") String id);
}
